<template>
	<scroll-view scroll-y="true">
		<view class="content">
			<view class="search-block">
				<text class="iconfont icon-search" style="color: #9A9A9A;font-size: 25rpx;margin-right: 20rpx;"></text>
				<input type="text" @input="onInput" value="" placeholder="搜索店铺查找" v-model="query" class="search-text"
					maxlength="10" focus placeholder-class="placeholder"/>
			</view>
			<view style="background-color: #ffffff;margin-top: 30rpx;">
				<view class="tabs">
					<view style="width: 100rpx;"></view>
					<view :class="['tabs-item',index==0?'tabs-item-tag':'']" @click="chooseTab(0)">全部门店</view>
					<view style="width: 120rpx;"></view>
					<view :class="['tabs-item',index==1?'tabs-item-tag':'']" @click="chooseTab(1)">常去门店</view>
					<view style="width: 100rpx;"></view>
				</view>
				<view :class="[index==0?'item':'']" v-show="index==0">
					<view class="video-list">
						<view v-if="mendian.length === 0" style="margin-top: 250rpx;">
							<view class="nodata">
								<image src="../../static/order/img_placeholder_nodata.png"></image>
							</view>
							<view class="nodatatxt">
								<text>暂无数据</text>
							</view>
						</view>
						
						<!--  v-html="brightenKeyword(item.name,query)" -->
						<view v-if="mendian.length > 0" class="video-content" v-for="(item, ind) in mendian" :key="ind"
							@click="goDevice(item.id,item.name)">
							<view class="storelist">
								<image :src="item.img1"></image>
							</view>
							<view class="video-describe">
								<view class="video-tit">
									{{item.name}}
								</view>
								<view style="display: flex;width: 210rpx;">
									<view class="online" v-if="item.onlineStatus=='正在营业'">
										<text>正在营业</text>
									</view>
									<view class="renovation" v-if="item.onlineStatus=='正在装修'">
										<text>正在装修</text>
									</view>
									<view class="renovation" v-if="item.onlineStatus=='暂未营业'">
										<text>暂未营业</text>
									</view>
									<view class="tag">
										<text>接收预订</text>
									</view>
								</view>
								<view style="height: 28rpx;"></view>
								<!-- 地址时间开始 -->
								<view class="video-reader">
									<!-- 时间 -->
									<text class="iconfont" style="font-size: 24rpx;color: #828282;">&#xe603;</text>
									<text class="timeoraddress">营业时间:{{item.onlineTime}}</text>
								</view>
								<view class="video-reader">
									<!-- 地址 -->
									<text class="iconfont" style="font-size: 24rpx;color: #828282;">&#x10132;</text>
									<text class="timeoraddress">{{item.addressLat}}</text>
								</view>
							</view>
							<view class="right">
								<view class="righticon">
									<text class="iconfont" @click.stop="toAddress(item.addressLat,item.name,item.longitude,item.latitude)">&#xe632;</text>
									<text class="iconfont" style="margin-left: 20rpx;" @click.stop="call(item.servicePhone)">&#xe60d;</text>
								</view>
								<view class="distance">
									<text>{{item.distance}}km</text>
								</view>
							</view>
						</view>
						<view style="height: 100rpx;"></view>
					</view>
				</view>
				<view :class="[index==1?'item':'']" v-show="index==1">
					<view class="video-list">
						<view v-if="changqu.length === 0" style="margin-top: 250rpx;">
							<view class="nodata">
								<image src="../../static/order/img_placeholder_nodata.png"></image>
							</view>
							<view class="nodatatxt">
								<text>暂无数据</text>
							</view>
						</view>
						
						<view if="changqu.length > 0" class="video-content" v-for="(item, ind) in changqu" :key="ind"
							@click="goDevice(item.id,item.name)">
							<view class="storelist">
								<image :src="item.img1"></image>
							</view>
							<view class="video-describe">
								<view class="video-tit">
									{{item.name}}
								</view>
								<view style="display: flex;width: 210rpx;">
									<view class="online" v-if="item.onlineStatus=='正在营业'">
										<text>正在营业</text>
									</view>
									<view class="renovation" v-if="item.onlineStatus=='正在装修'">
										<text>正在装修</text>
									</view>
									<view class="tag">
										<text>接收预订</text>
									</view>
								</view>
								<view style="height: 28rpx;"></view>
								<!-- 地址时间开始 -->
								<view class="video-reader">
									<!-- 时间 -->
									<text class="iconfont" style="font-size: 24rpx;color: #828282;">&#xe603;</text>
									<text class="timeoraddress">营业时间:{{item.onlineTime}}</text>
								</view>
								<view class="video-reader">
									<!-- 地址 -->
									<text class="iconfont" style="font-size: 24rpx;color: #828282;">&#x10132;</text>
									<text class="timeoraddress">{{item.addressLat}}</text>
								</view>
							</view>
							<view class="right">
								<view class="righticon">
									<text class="iconfont" @click.stop="toAddress(item.addressLat,item.name,item.longitude,item.latitude)">&#xe632;</text>
									<text class="iconfont" style="margin-left: 20rpx;" @click.stop="call(item.servicePhone)">&#xe60d;</text>
								</view>
								<view class="distance">
									<!-- <text>{{item.distance}}km</text> -->
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</scroll-view>
</template>

<script>
	import Api from '@/common/api';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		formatDate
	} from '@/common/date';
	export default {
		components: {

		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		data() {
			return {
				index: 0,
				mendian: [],
				changqu: [],
				type: '',
				query: ''
			}
		},
		onLoad(options) {
			if (typeof(options.type) != 'undefined') {
				console.log(options.type)
				this.type = options.type
				// if (this.type == 'chongzhi') {
				// 	uni.showModal({
				// 		title: '提示',
				// 		content: '请选择需要充值的门店',
				// 		showCancel: false,
				// 		success: (e) => {
				// 			if (e.confirm) {}
				// 		}
				// 	})
				// }
				// if (this.type == 'duihuan') {
				// 	uni.showModal({
				// 		title: '提示',
				// 		content: '请选择兑换的门店',
				// 		showCancel: false,
				// 		success: (e) => {
				// 			if (e.confirm) {}
				// 		}
				// 	})
				// }
			}
			this.getAllStore();
		},
		onShow() {
			this.getLocation();
		},
		methods: {
			// brightenKeyword(val, editKeyword) {
			// 	const Reg = new RegExp(editKeyword);
			// 	if (val) {
			// 		const res = val.replace(Reg, `<span style="color: #D7311E;">${editKeyword }</span>`);
			// 		return res;
			// 	}
			// },
			onInput(e) {
				let query = e.detail.value;
				this.getStoreByQuery(query);
			},
			call(mobile) {
				if (typeof(mobile) == 'undefined' || mobile == '') {

				} else {
					uni.makePhoneCall({
						phoneNumber: mobile,
						success: function() {
							console.log('拨打电话成功');
						},
						fail() {
							console.log('打电话失败了');
						}
					})
				}
			},
			// 定位授权
			getLocation() {
				let that = this;
				// 1、判断手机定位服务【GPS】 是否授权
				uni.getSystemInfo({
					success(res) {
						console.log("判断手机定位服务是否授权:", res);
						let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
						let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
						if (locationEnabled == false || locationAuthorized == false) {
							//手机定位服务（GPS）未授权
							uni.showToast({
								title: "请打开手机GPS",
								icon: "none",
							});
						} else {
							//手机定位服务（GPS）已授权
							// 2、判断微信小程序是否授权位置信息
							// 微信小程序已授权位置信息
							uni.authorize({
								//授权请求窗口
								scope: "scope.userLocation", //授权的类型
								success: (res) => {
									that.getLocationInfo();
								},
								fail: (err) => {
									err = err["errMsg"];
									uni
										.showModal({
											content: "需要授权位置信息",
											confirmText: "确认授权",
										})
										.then((res) => {
											console.log(res);
											if (res[1]["confirm"]) {
												uni.openSetting({
													success: (res) => {
														if (res.authSetting[
																"scope.userLocation"
															]) {
															// 授权成功
															uni.showToast({
																title: "授权成功",
																icon: "none",
															});
															that.getLocationInfo();
														} else {
															// 未授权
															uni.showToast({
																title: "授权失败,请重新授权",
																icon: "none",
															});
															uni.showModal({
																title: "授权",
																content: "获取授权" +
																	authouName +
																	"失败,是否前往授权设置？",
																success: function(
																	result
																) {
																	if (result
																		.confirm
																	) {
																		uni
																			.openSetting();
																	}
																},
																fail: function() {
																	uni.showToast({
																		title: "系统错误！",
																		icon: "none",
																	});
																},
															});
														}
													},
												});
											}
											if (res[1]["cancel"]) {
												// 取消授权
												uni.showToast({
													title: "你拒绝了授权，无法获得周边信息",
													icon: "none",
												});
											}
										});
								},
								complete(res) {
									// console.log('授权弹框', res);
									if (res.errMsg == "authorize:ok") {
										that.getLocationInfo();
									} else {
										uni.showModal({
											title: "授权",
											content: "获取授权" + authouName + "失败,是否前往授权设置？",
											success: function(result) {
												if (result.confirm) {
													uni.openSetting();
												}
											},
											fail: function() {
												uni.showToast({
													title: "系统错误！",
													icon: "none",
												});
											},
										});
									}
								},
							});
						}
					},
				});
			},
			getLocationInfo() {
				let that = this;
				uni.getLocation({
					type: 'gcj02',
					success(res) {
						console.log(res);
						uni.setStorageSync('longitude', res.longitude);
						uni.setStorageSync('latitude', res.latitude);
						that.getAllStore();
					},
					fail: function(res) {
						console.log(res);
					}
				});
			},
			chooseTab(index) {
				this.index = index;
				if (index == 0) {
					this.getAllStore();
				} else {
					this.getChangqu();
				}
			},
			async getAllStore() {
				let params = {
					longitude: uni.getStorageSync('longitude'),
					latitude: uni.getStorageSync('latitude')
				}
				console.log(params)
				if (typeof uni.getStorageSync('longitude') == 'undefined') {
					params = {}
				}
				let result = await Api.apiCall('get', Api.qsg.getAllStore, params);
				if (result) {
					console.log(result)
					this.mendian = result;
				}
			},
			async getStoreByQuery(text) {
				let params = {
					longitude: uni.getStorageSync('longitude'),
					latitude: uni.getStorageSync('latitude'),
					storeName: text
				}
				console.log(params)
				if (typeof uni.getStorageSync('longitude') == 'undefined') {
					params = {}
				}
				let result = await Api.apiCall('get', Api.qsg.getAllStore, params);
				if (result) {
					console.log(result)
					this.mendian = result;
				}
			},
			toAddress(address,name,longitude,latitude){
			//此处item是 你获取到的当前点击的那一条数据  方法里面需要用到 名称  地址 经纬度
					uni.openLocation({
			            latitude: Number(latitude),  //目标纬度
			            longitude: Number(longitude),  //目标经度
			            name: name, //名称
			            address: address, //地址
			            scale: 28
			          });
			},
			async getChangqu() {
				let params = {
					longitude: uni.getStorageSync('longitude'),
					latitude: uni.getStorageSync('latitude')
				}
				if (typeof uni.getStorageSync('longitude') == 'undefined') {
					params = {}
				}
				let result = await Api.apiCall('get', Api.qsg.bestStore, params);
				if (result) {
					console.log(result)
					this.changqu = result;
				}
			},
			goDevice(id, name) {
				console.log(this.type)
				if (this.type == 'chongzhi') {
					uni.navigateTo({
						url: '/pages/money/recharge?storeId=' + id
					})
				} else if (this.type == 'duihuan') {
					let params = {
						id: id,
						name: name
					}
					uni.$emit('store', params);
					uni.navigateBack({
						delta: 1
					})
				} else {
					uni.navigateTo({
						url: '/pages/meallist/selectRoom?storeId=' + id
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		width: 100%;
		overflow-x: hidden
	}

	.priceSign {
		font-size: 24rpx;
	}

	.priceNum {
		font-size: 32rpx;
		font-weight: bold;
	}

	.priceIcon {
		font-size: 24rpx;
	}

	.schoolName {
		width: 750rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		font-size: 50rpx;
		font-weight: bold;
		color: #000000;
	}

	.uni-input {
		padding: 0 24rpx;
	}

	.choese {
		width: 750rpx;
		display: flex;
		box-sizing: border-box;
		padding: 0 30rpx;
		color: #888888;
	}

	.dataName {
		height: 60rpx;
	}

	.currentSchool {
		height: 60rpx;
		background: #FFFFFF;
	}

	.video-tit {
		font-size: 30rpx;
		color: #000000;
		font-weight: bolder;
		display: inline-block;
		vertical-align: top;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.checkMore text {
		display: block;
		width: 100%;
		height: 60rpx;
		line-height: 58rpx;
		font-size: 24rpx;
		color: #172a89;
		border: 1px solid #172A89;
		text-align: center;
		box-sizing: border-box;
		margin-top: 46rpx;
		border-radius: 50rpx;
	}

	.checkMore {
		font-size: 24rpx;
		color: #172a89;
		flex: 1;
	}

	.video-price {
		font-size: 30rpx;
		color: #666;
		overflow: hidden;
		display: inline-block;
		vertical-align: top;
		text-align: center;
		box-sizing: border-box;
	}

	.video-reader {
		font-size: 22rpx;
		font-weight: 200;
		color: #666;
		letter-spacing: 2rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		-o-text-overflow: ellipsis;
	}
	
	.placeholder{
		font-size: 24rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #9A9A9A;
	}
	
	.renovation {
		width: 100rpx;
		height: 28rpx;
		flex: 1;
		background: #E8E8E8;
		border-radius: 4rpx;
		font-size: 20rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #9A9A9A;
		line-height: 28rpx;
		text-align: center;
	}
	
	.tag {
		width: 100rpx;
		height: 28rpx;
		flex: 1;
		text-align: center;
		background: linear-gradient(90deg, #FFE2AE 0%, #E3B97F 100%);
		border-radius: 4rpx;
		font-size: 20rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #492A00;
		line-height: 28rpx;
		float: right;
		margin-left: 10rpx;
	}
	
	.online {
		width: 100rpx;
		height: 28rpx;
		flex: 1;
		background: #E3F6E4;
		border-radius: 4rpx;
		font-size: 20rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #0DA944;
		line-height: 28rpx;
		text-align: center;
	}

	/* 搜索框 */
	.search-ico-1 {
		width: 40upx;
		height: 40upx;
		float: right;
	}

	.search-text {
		font-size: 24rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #1A1A1A;
		background-color: #FFFFFF;
		height: 60upx;
		width: 580rpx;
	}

	.search-block {
		width: 686rpx;
		height: 64rpx;
		line-height: 64rpx;
		display: flex;
		flex-direction: row;
		padding-left: 30upx;
		position: relative;
		top: 30upx;
		-moz-box-shadow: 0 0 10px #e6e6e6;
		-webkit-box-shadow: 0 0 10px #e6e6e6;
		box-shadow: 0 0 10px #e6e6e6;
		border: 1px solid #E8E8E8;
		border-radius: 32rpx;
		margin-bottom: 30rpx;
		margin-left: 32rpx;
	}

	.search-ico-wapper {
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 0upx 0upx 0upx 40upx;
		border-bottom-left-radius: 18px;
		border-top-left-radius: 18px;
	}

	.search-ico-wapper1 {
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 0upx 40upx 0upx 0upx;
		border-bottom-right-radius: 18px;
		border-top-right-radius: 18px;
	}
	
	.timeoraddress {
		height: 21rpx;
		line-height: 21rpx;
		margin-left: 8rpx;
		font-size: 24rpx;
		font-family: Source Han Sans CN;
		color: #828282;
	}
	
	.right {
		width: 130rpx;
	}
	
	.righticon {
		color: #0DA944;
		margin-top: 33rpx;
		margin-left: 10rpx;
	}
	
	.nodata{
		image{
			height: 170rpx;
			width: 170rpx;
		}
		text-align: center;
	}
	
	.nodatatxt{
		font-size: 24rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #9A9A9A;
		text-align: center;
	}
	
	.distance {
		font-size: 22rpx;
		font-family: Source Han Sans CN;
		font-weight: 500;
		color: #555555;
		height: 18rpx;
		line-height: 18rpx;
		margin-left: 15rpx;
		margin-top: 105rpx;
	}

	.shadow {
		width: 638upx;
		height: 60upx;
		border-radius: 18px;
		-moz-box-shadow: 0 0 10px #e6e6e6;
		-webkit-box-shadow: 0 0 10px #e6e6e6;
		box-shadow: 0 0 10px #e6e6e6;
		position: relative;
		top: -90upx;
		left: 60upx;
	}

	.video-describe {
		display: inline-block;
		vertical-align: top;
		width: 380rpx;
		padding-top: 28rpx;
		padding-left: 15rpx;
	}

	.video-cover {
		width: 100rpx;
		height: 100rpx;
		margin: 30rpx 30rpx 30rpx 0;
		display: inline-block;
		vertical-align: top;
		border-radius: 50%;
		flex: 1;
	}

	.video-cover image {
		width: 100rpx;
		height: 100rpx;
		background: #FFFFFF;
		border-radius: 50%;
	}

	.video-content {
		width: 100%;
		height: 256rpx;
		display: flex;
		// padding-left: 80rpx;
		// padding-top: 10rpx;
	}
	
	.storelist {
		flex: 232rpx;
		padding-left: 32rpx;
		padding-top: 28rpx;
	
		image {
			width: 200rpx;
			height: 200rpx;
			border-radius: 8rpx;
		}
	}

	.video-list {
		width: 100%;
		height: auto;
		box-sizing: border-box;
	}

	.content {
		background-color: #FFFFFF;
		.tabs {
			height: 60rpx;
			display: flex;
			margin-top: 10rpx;

			.tabs-item {
				// 平均分布
				flex: 1;
				text-align: center;
				color: #111111;
				height: 60rpx;
				line-height: 60rpx;
			}

			.tabs-item-tag {
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #1A1A1A;
				border-bottom: 4rpx solid #0DA944;
			}
		}

		.item {
			padding: 10rpx;
			display: grid;
			text {
				text-align: center;
			}
		}
	}
</style>